package net.falcon.data;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;

public class GlassWall {

	private Location loc;
	private Wall orient;
	private Player owner;

	public GlassWall(Player o, Location l, Wall orientation) {
		loc = l;
		owner = o;
		orient = orientation;
	}

	public void setLoc(Location l) {
		removeGlass();
		loc = l;
		placeGlass();
	}


	//xxx
	//xxx
	//xxx
	public void removeGlass() {
		Location locat = loc.clone();

		if(orient == Wall.EW) {
			for(int x = -1; x <= 1; x++) { //do this 3 times, incrementing X each time
				locat.setX(loc.getX() + x);
				for(int y = 0; y <= 2; y++) { //do this 3 times, incrementing Y each time
					locat.setY(loc.getY() + y);
					owner.sendBlockChange(locat, owner.getWorld().getBlockAt(locat).getType(), owner.getWorld().getBlockAt(locat).getData());
				}
			}
		}

		if(orient == Wall.NS) {

			for(int z = -1; z <= 1; z++) { //do this 3 times, incrementing X each time
				locat.setZ(loc.getZ() + z);
				for(int y = 0; y <= 2; y++) { //do this 3 times, incrementing Y each time
					locat.setY(loc.getY() + y);
					owner.sendBlockChange(locat, owner.getWorld().getBlockAt(locat).getType(), owner.getWorld().getBlockAt(locat).getData());
				}
			}
		}

	}

	public void placeGlass() {
		Location locat = loc.clone();

		if(orient == Wall.EW) {
			for(int x = -1; x <= 1; x++) { //do this 3 times, incrementing X each time
				locat.setX(loc.getX() + x);
				for(int y = 0; y <= 2; y++) { //do this 3 times, incrementing Y each time
					locat.setY(loc.getY() + y);
					if(!owner.getWorld().getBlockAt(locat).getType().isSolid()) {
						owner.sendBlockChange(locat, Material.GLASS, (byte)0);
					}
				}
			}
		}

		if(orient == Wall.NS) {

			for(int z = -1; z <= 1; z++) { //do this 3 times, incrementing X each time
				locat.setZ(loc.getZ() + z);
				for(int y = 0; y <= 2; y++) { //do this 3 times, incrementing Y each time
					locat.setY(loc.getY() + y);
					if(!owner.getWorld().getBlockAt(locat).getType().isSolid()) {
						owner.sendBlockChange(locat, Material.GLASS, (byte)0);
					}
				}
			}
		}

	}

	public enum Wall {
		NS, //Xchg, Zsame
		EW,  //Xsame, Zchg
	}

}
